/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yaps.petstore.dao.impl;

import com.yaps.petstore.dao.CategoryDAO;
import com.yaps.petstore.dao.OrderDAO;
import com.yaps.petstore.dto.CartItem;
import com.yaps.petstore.entity.catalog.Item;
import com.yaps.petstore.entity.catalog.Product;
import com.yaps.petstore.entity.customer.Address;
import com.yaps.petstore.entity.customer.Customer;
import com.yaps.petstore.entity.order.CreditCard;
import com.yaps.petstore.entity.order.Order;
import com.yaps.petstore.entity.order.OrderLine;
import com.yaps.petstore.exception.ValidationException;
import com.yaps.petstore.util.Constants;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("OrderDAO")
public class OrderDAOImpl implements  OrderDAO {
	@PersistenceContext
	private EntityManager em;
        
    private final String cname = this.getClass().getName();
    private Logger logger = Logger.getLogger(Constants.LOGGER_STATELESS);

    @Override
    public Order findOrder(Long orderId) {
        return em.find(Order.class, orderId);
    }

    @Override
    @Transactional
    public void deleteOrder(Order order) {
        em.remove(em.merge(order));
    }

    @Override
    public List<Order> findOrders() {
     
        Query query;
        List<Order> orders;
        query = em.createQuery("SELECT o FROM Order o");
        orders = query.getResultList();
        return orders;
    }

    @Override
    @Transactional
    public Order createOrder(Order order) {
        System.out.println("-----------------BEFORE----------------");  
        System.out.println(order); 
       em.persist(order);       
       System.out.println("-----------------AFTER----------------"); 
       System.out.println(order); 
        return order;
    }
    
    
}
